import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class MatrixTransforming
{
	public int[][] mat;
	
	public void push(int r, int c){
		for(int i=r-1; i<=r+1; i++){
			for(int j=c-1; j<=c+1; j++){
				mat[i][j] ^= 1;
			}
		}
	}
	public int minPushes(String[] a, String[] b)
	{
		int N = a.length;
		int M = a[0].length();
		mat = new int[N][M];
		for(int i=0; i<N; i++) for(int j=0; j<M; j++) mat[i][j] = a[i].charAt(j) - '0';
		int ret = 0;
		for(int i=0; i+2<N; i++){
			for(int j=0; j+2<M; j++){
				if(mat[i][j] != b[i].charAt(j)-'0'){
					push(i+1, j+1);
					ret++;
				}
			}
		}
		for(int i=0; i<N; i++) for(int j=0; j<M; j++)
			if(mat[i][j] != b[i].charAt(j)-'0')
				return -1;
		return ret;
	}
	
	public static void main(String[] args)
	{
		long time;
		int answer;
		boolean errors = false;
		int desiredAnswer;
		
		
		time = System.currentTimeMillis();
		answer = new MatrixTransforming().minPushes(new String[]{"111","111","111"}, new String[]{"000","000","000"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 1;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatrixTransforming().minPushes(new String[]{"1"}, new String[]{"0"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = -1;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatrixTransforming().minPushes(new String[]{"001","100","100","000","011","010","100","100","010","010","010","110","101","101","000","110","000","110"}, new String[]{"001","100","011","000","100","010","011","100","101","101","010","001","010","010","111","110","111","001"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 7;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatrixTransforming().minPushes(new String[]{"0000","0010","0000"}, new String[]{"1001","1011","1001"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 2;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		
		
		if (errors)
			System.out.println("Some of the test cases had errors :-(");
		else
			System.out.println("You're a stud (at least on the test data)! :-D ");
	}

}
//Powered by [KawigiEdit] 2.0!
